package com.yequan.thread.keyword.volatileword;

/**
 * @author : Administrator
 * @date : 2020/5/17
 */
public class VolatileFeatureExample2 {

    long v1 = 0L;

    public synchronized void set(long l) {
        v1 = l;
    }

    public void getAndIncrement() {
        long temp = get();
        temp += 1L;
        set(temp);
    }

    public synchronized long get() {
        return v1;
    }

    public static void main(String[] args) {
        VolatileFeatureExample2 volatileFeatureExample2 = new VolatileFeatureExample2();
        Thread thread1 = new Thread(() -> volatileFeatureExample2.set(1));

        Thread thread2 = new Thread(() -> volatileFeatureExample2.getAndIncrement());

        Thread thread3 = new Thread(() -> System.out.println(volatileFeatureExample2.get()));

        thread1.start();
        thread2.start();
        thread3.start();
    }

}
